package com.qf.petmall.starter.classify.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.petmall.starter.classify.entity.Classify;
import com.qf.petmall.starter.classify.service.IClassService;
import com.qf.petmall.starter.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类页面的内容模块 前端控制器
 * </p>
 *
 * @author Zhong_
 * @since 2022-12-06
 */
@CrossOrigin
@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    IClassService classService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param limit
     * @return
     */
    @GetMapping("/commonts")
    public R detailCommonts(Integer pageNum, Integer limit,String classClass) {

        // 创建分页对象
        final Page<Classify> page = new Page<>(pageNum, limit);

        LambdaQueryWrapper<Classify> eq = new LambdaQueryWrapper<Classify>().eq(Classify::getClassClass, classClass);

        // 分页查询
        classService.page(page,eq);

        final List<Classify> commentsList = page.getRecords();


        return R.ok().put("class", commentsList);

    }


    /**
     * 根据价格降序
     * @return
     */
    @GetMapping("/price/{flag}/{classClass}")
    public R getByPrice(@PathVariable("flag") boolean flag,@PathVariable("classClass") String classClass){

        LambdaQueryWrapper<Classify> queryWrapper = new LambdaQueryWrapper<Classify>()
                .eq(Classify::getClassClass,classClass)
                .orderBy(true,flag,Classify::getPrice);

        List<Classify> list = classService.list(queryWrapper);

        return R.ok().put("class",list);
    }

    /**
     * 根据销量降序
     * @return
     */
    @GetMapping("/sold/{flag}/{classClass}")
    public R getBySold(@PathVariable("flag") boolean flag,@PathVariable("classClass") String classClass){

        LambdaQueryWrapper<Classify> queryWrapper = new LambdaQueryWrapper<Classify>()
                .eq(Classify::getClassClass,classClass)
                .orderBy(true,flag,Classify::getSold);

        List<Classify> list = classService.list(queryWrapper);

        return R.ok().put("class",list);
    }

    /**
     * 根据时间降序
     * @return
     */
    @GetMapping("/time/{flag}/{classClass}")
    public R getByTime(@PathVariable("flag") boolean flag,@PathVariable("classClass") String classClass){

        LambdaQueryWrapper<Classify> queryWrapper = new LambdaQueryWrapper<Classify>()
                .eq(Classify::getClassClass,classClass)
                .orderBy(true,flag,Classify::getTime);

        List<Classify> list = classService.list(queryWrapper);

        return R.ok().put("class",list);
    }
}
